package com.demo.java8.stream;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by churcy on 2018/3/22.
 */
public class TestStream {

    public static void main(String[] args) {


        List<Dish> menus = Arrays.asList(
                new Dish("prok", false, 800, Dish.Type.MEAT),
                new Dish("beef", false, 700, Dish.Type.MEAT),
                new Dish("chicken", false, 400, Dish.Type.MEAT),
                new Dish("french fries", true, 530, Dish.Type.OTHER),
                new Dish("rice", true, 350, Dish.Type.OTHER),
                new Dish("season fruit", true, 120, Dish.Type.OTHER),
                new Dish("pizza", true, 550, Dish.Type.OTHER),
                new Dish("prawns", false, 300, Dish.Type.FISH),
                new Dish("salmon", false, 450, Dish.Type.FISH)
        );


        List<String> lowCaloricDishesName = menus.stream().filter(d -> d.getCalories() < 400).sorted(Comparator.comparing(d -> d.getCalories())).map(d -> d.getName()).collect(Collectors.toList());

        System.out.println(lowCaloricDishesName);

        System.out.println("---------------the next demo-------------------");

        List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
        List<Integer> squares = numbers.stream().map(n -> n * n).collect(Collectors.toList());
        System.out.println(squares);

        System.out.println("---------------the next demo-------------------");

        List<Integer> numbersOne = Arrays.asList(1, 2, 3);
        List<Integer> numbersTwo = Arrays.asList(3, 4);

        List<int[]> pairs = numbersOne.stream().flatMap(i -> numbersTwo.stream().map(j -> new int[]{i, j})).collect(Collectors.toList());
        System.out.println(pairs);

        System.out.println("---------------the next demo-------------------");

        List<int[]> pairsOne = numbersOne.stream().flatMap(i -> numbersTwo.stream().filter(j -> (i + j) % 3 == 0).map(j -> new int[]{i, j})).collect(Collectors.toList());
        System.out.println(pairsOne);
    }
}
